<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

/**
 * 基础数据类
 * @author Administrator
 * 交易所与合约 模板
 */
class EssentialData extends Common
{
    /**
     * 写入日志
     * @param unknown $operation_category
     * @param unknown $detail_name
     * @param unknown $operation_value
     * @param unknown $re_name
     * @param unknown $re_value
     */
    public function putRizhi($operation_category,$detail_name,$operation_value,$re_name,$re_value){
        $arr = array_combine($detail_name, $operation_value);
        $redis = array_combine($re_name, $re_value);
        $this->riZhi($operation_category, $arr, $redis);
    }
    
	// 合约列表
	public function hy()
	{
		$result = db('heyue')->select();
		foreach ($result as $k => $v){
		    $result[$k]['youxiao_shijian'] = date("Y-m-d H:i:s",$v['youxiao_shijian']);
		}
		returnSuccess($result);
	}
	
	// 交易時間
	public function jysj()
	{
		$jiaoyi = db('jiaoyi')
		->alias('j')
		->join("jiaoyi_zu z","z.id=j.zu_id","LEFT")
		->field("j.zu_id,j.shijian,j.shijian1,j.id,j.beizhu,j.lb,z.name as jiaoyi_zu_name")
		->order("j.zu_id,j.id")
		->select();
		foreach($jiaoyi as $key => $val){
		    $jiaoyi[$key]['shijian'] = date("H:i:s",$val['shijian']);
		    $jiaoyi[$key]['shijian1'] = date("H:i:s",$val['shijian1']);
			if($val['beizhu'] == null) $jiaoyi[$key]['beizhu'] = '';
		}
        returnSuccess($jiaoyi);
	}
	
    // 交易所
    public function jys()
	{
		$result = db('shichang')->field("id,bh,name")->select();
		returnSuccess($result);
	}
	
	// 品种
	public function pz()
    {
        $result_heyue_zu = db('heyue_zu')
            ->alias('he')
            ->join('jiaoyi_zu j',"he.jiaoyi_zu_id=j.id","LEFT")
            ->join("ib_heyue_kongzhi i","i.heyue_zu_id=he.id and i.ib_id=".$this->gt_jujian_id,"LEFT")
            ->field("he.name,he.bh,he.id,he.heyue_cs,he.bodong,he.danwei,he.beizhu,he.ping_shijian,he.ping_shijian1,he.cancel_shijian,he.jiaoyi_zu_id,he.hb_id,j.name as jiaoyi_zu_name,i.bz as ib_bz")
            ->select();
        foreach ($result_heyue_zu as $k => $v){
            $result_heyue_zu[$k]['cancel_shijian'] = date("H:i:s",$v['cancel_shijian']);
            $result_heyue_zu[$k]['ping_shijian'] = date("H:i:s",$v['ping_shijian']);
            $result_heyue_zu[$k]['ping_shijian1'] = date("H:i:s",$v['ping_shijian1']);
            if ($v['hb_id'] == 1){
                $result_heyue_zu[$k]['hb_name'] = "人民币";
            }elseif ($v['hb_id'] == 2){
                $result_heyue_zu[$k]['hb_name'] = "港币";
            }elseif ($v['hb_id'] == 3){
                $result_heyue_zu[$k]['hb_name'] = "美元";
            }elseif ($v['hb_id'] == 4){
                $result_heyue_zu[$k]['hb_name'] = "欧元";
            }
            if ($v['ib_bz'] == 1) {
                $result_heyue_zu[$k]['is_start'] = '启用';
            } elseif ($v['ib_bz'] == 0) {
                $result_heyue_zu[$k]['is_start'] = '未启用';
            } else {
                $result_heyue_zu[$k]['is_start'] = '未启用';
            }
        }
        returnSuccess($result_heyue_zu);
    }
    
    // 修改品种 配置
    public function pz_edit()
    {
        $data = input();
        $result = array();
        $ib_db = db("ib_heyue_kongzhi");
        $ib_id = db('jujian')
            ->where('id', $this->id)
            ->value('gt_jujian_id');
        $heyue_has = $ib_db
            ->where("heyue_zu_id",$data['id'])
            ->where("ib_id",$ib_id)
            ->find();
        if($heyue_has){
            if($data['bz'] == $heyue_has['bz']){
                returnError('您已配置过该项，请勿重复操作');
            }else{
                $pz_edit = $ib_db
                    ->where("heyue_zu_id",$data['id'])
                    ->where("ib_id",$ib_id)
                    ->update(['bz'=>$data['bz']]);
                if($pz_edit){
                    $this->riZhi(39,['添加用户'=>'成功','操作人员'=>$this->jujian['bh'],'heyue_zu_id'=>$data['id'],'bz'=>$data['bz']],['update_bz'=>'1']);
                    returnSuccess([],'配置成功');
                }else{
                    returnError("操作失败，请稍后重试");
                }
            }
        }else{
            returnError("数据信息错误，请重试");
        }
    }
	
    /************手续费模板开始***********/
	//加载手续费模板组
	public function sxfmb_left()
	{
		$result = db('fee_yonghu_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}
	
	// 手续费模板添加模板
	public function sxfmb_tjmb()
	{
		$data['name'] = input('name','','trim');
		$data['beizhu'] = input('beizhu','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		if (!$data['beizhu']) returnError("请填写说明");
		$sxf_all = db('fee_yonghu_zu')
            ->where('name',$data['name'])
            ->where("bz",1)
            ->count();
		if ($sxf_all>0){
            returnError("此模板名称已被占用");
        }else {
            $data['bz']=1;
            $data['ib_id'] = $this->gt_jujian_id;
    		$result = db('fee_yonghu_zu')->insertGetId($data);
            if($result>0){
                $operation_category = 40;
                $detail_name = array('添加手续费模板','手续费模板ID','手续费模板名称','手续费模板说明');
                $operation_value = array('成功',$result,$data['name'],$data['beizhu']);
                $re_name = ['sxfmb_add'];
                $re_value = ['1'];
                $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
                returnSuccess([],"操作成功");
            }else{
                returnError("操作失败");
            }
        }
    }

    // 手续费模板删除
	public function sxfmb_scmb()
	{
	    $data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
        $judge = db('yonghu')
                ->where('fee_zu_id',$data['id'])
                ->count();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('fee_yonghu_zu')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result){
                $this->riZhi(103,['删除手续费模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>0],['update_bz'=>'1']);
                returnSuccess([],"操作成功");
            }else{
                returnError("操作失败");
            }
        }
	}
	
	// 手续费模板加载数据
	public function sxfmb_jzsj()
	{
		$data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
		$res = array();
		$heyue_new = db('heyue_zu')->field('id,bh,name')->select();
        $result_fee_yonghu_zu_detail = db('fee_yonghu_zu_detail')->where('fee_zu_id',$data['id'])->select();
        if(!empty($result_fee_yonghu_zu_detail)){
            //两联表查询
            $res = db('fee_yonghu_zu_detail')
                ->alias('de')
                ->join('heyue_zu he','de.heyue_zu_id = he.id')
                ->where('de.fee_zu_id',$data['id'])
                ->field('de.heyue_zu_id,de.jian_yongjin,de.ping_zuo_yongjin,de.ping_jin_yongjin,de.geye_fee,de.toucun_bili,he.name,he.bh,he.id')
                ->select();
            //遍历出来表中的heyue_id
            $detail_number = array();
            foreach($res as $x=>$val)
            {
                $detail_number[$x] = $val['heyue_zu_id'];
            }
            //查出不存在fee_jujian_zu_detail表中的数据
            $heyue_new = db('heyue_zu')
                ->where('id','not in',$detail_number)
                ->field('id,bh,name')
                ->select();
        }
        foreach($heyue_new as $x=>$val)
        {
            $heyue_new[$x]['jian_yongjin'] = 0;
            $heyue_new[$x]['ping_zuo_yongjin'] = 0;
            $heyue_new[$x]['ping_jin_yongjin'] = 0;
            $heyue_new[$x]['geye_fee'] = 0;
            $heyue_new[$x]['toucun_bili'] = 0;
        }
        $return = array_merge($res,$heyue_new);
        returnSuccess($return);
	}
	
	// 修改手续费模板
	public function sxfmb_xgmb()
	{
		$data['id'] = input('id/d');
		$data['name'] = input("name","","trim");
		$data['beizhu'] = input('beizhu',"","trim");
		if (!$data['name']) returnError("模板名称不能为空");
		if (!$data['beizhu']) returnError("请填写说明");
		if (!$data['id']) returnError("参数错误");
		$res = db('fee_yonghu_zu')->find($data['id']);
		if ($res){
		    if ($data['name'] != $res['name'] && db('fee_yonghu_zu')->where("name",$data['name'])->where("bz",1)->find()) returnError("模板名称已存在");
		    if ($data['beizhu'] != $res['beizhu'] || $data['name'] != $res['name']){
		        $result = db('fee_yonghu_zu')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		        if($result>0){
		            $operation_category = 41;
		            $detail_name = array('编辑手续费模板','手续费模板ID','手续费模板名称','手续费模板说明');
		            $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
		            $re_name = ['sxfmb_edit'];
		            $re_value = ['1'];
		            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
		            returnSuccess([],"操作成功");
		        }else {
		            returnError("操作失败");
		        }
		    }else {
		        returnSuccess([],"操作成功");
		    }
		}else {
		    returnError("操作失败");
		}
	}
    
	// 修改手续费数据 获取当前数据内容
	public function scsxfsj(){
		$data = input();
		$result_detail = db('fee_yonghu_zu_detail')
		->alias("fe")
		->join("heyue_zu hz","hz.name='{$data['name']}' and hz.id=heyue_zu_id")
		->where('fe.fee_zu_id',$data['moban_id'])
		->field('fe.*,hz.name,hz.bh')
		->find();
		if(empty($result_detail)){
			$result = db('heyue_zu')->where('name',$data['name'])->field("id,name,bh")->find();
			$result_detail = array(
			    'fee_zu_id'       => $data['moban_id'],
			    'heyue_zu_id'     => $result['id'],
			    'jian_yongjin'    => 0,
			    'ping_jin_yongjin'=> 0,
			    'ping_zuo_yongjin'=> 0,
			    'geye_fee'        => 0,
			    'toucun_bili'     => 0,
			    'name'            => $result['name'],
			    'bh'              => $result['bh']
			);
		}
		returnSuccess($result_detail);
	}
	
	// 确认修改手续费数据
	public function tjxgsxfsj(){
		$data['heyue_zu_id'] = input('id/d');
		$data['fee_zu_id'] = input('fee_zu_id/d');
		$data['geye_fee'] = input('geye_fee','','trim');
		$data['jian_yongjin'] = input('jian_yongjin','','trim');
		$data['ping_jin_yongjin'] = input('ping_jin_yongjin','','trim');
		$data['ping_zuo_yongjin'] = input('ping_zuo_yongjin','','trim');
		if (!$data['heyue_zu_id'] || !$data['fee_zu_id']) returnError("缺少重要参数");
		if (!is_numeric($data['geye_fee']) || !is_numeric($data['jian_yongjin']) || !is_numeric($data['ping_jin_yongjin']) || !is_numeric($data['ping_zuo_yongjin'])) returnError("数据格式错误");
        $have = db('fee_yonghu_zu_detail')
            ->where('heyue_zu_id',$data['heyue_zu_id'])
            ->where('fee_zu_id',$data['fee_zu_id'])
            ->count();
		if($have>0){
		    $minInfo = db("yonghu")
		    ->alias("y")
		    ->join("jujian j","y.jujian_id=j.id and j.fee_zu_id!=y.fee_zu_id","LEFT")
		    ->join("fee_jujian_zu_detail fd","j.fee_zu_id=fd.fee_zu_id and fd.heyue_zu_id={$data['heyue_zu_id']}")
		    ->where('y.fee_zu_id',$data['fee_zu_id'])
		    ->field("max(fd.jian_yongjin) as jian_yongjin,max(fd.ping_jin_yongjin) as ping_jin_yongjin,max(fd.ping_zuo_yongjin) as ping_zuo_yongjin")
		    ->find();
		    if ($data['jian_yongjin'] < $minInfo['jian_yongjin'] && $minInfo['jian_yongjin'] != 0 && $data['jian_yongjin'] >= 0){
		        returnError("开仓手续费不能小于上级({$minInfo['jian_yongjin']})");
		    }
		    if ($data['ping_jin_yongjin'] < $minInfo['ping_jin_yongjin'] && $minInfo['ping_jin_yongjin'] != 0 && $data['ping_jin_yongjin'] >= 0){
		        returnError("平金手续费不能小于上级({$minInfo['ping_jin_yongjin']})");
		    }
		    if ($data['ping_zuo_yongjin'] < $minInfo['ping_zuo_yongjin'] && $minInfo['ping_zuo_yongjin'] != 0 && $data['ping_zuo_yongjin'] >= 0){
		        returnError("平左手续费不能小于上级({$minInfo['ping_zuo_yongjin']})");
		    }
            $result = db('fee_yonghu_zu_detail')
                ->where('heyue_zu_id',$data['heyue_zu_id'])
                ->where('fee_zu_id',$data['fee_zu_id'])
                ->update(['jian_yongjin'=>$data['jian_yongjin'],'ping_jin_yongjin'=>$data['ping_jin_yongjin'],'ping_zuo_yongjin'=>$data['ping_zuo_yongjin'],'geye_fee'=>$data['geye_fee']]);
            if($result){
                $operation_category = 103;
                $detail_name = array('编辑手续费项目','手续费模板ID','合约全局ID','开仓','平今','平仓','手续费');
                $operation_value = array('成功',$data['fee_zu_id'],$data['heyue_zu_id'],$data['jian_yongjin'],$data['ping_jin_yongjin'],$data['ping_zuo_yongjin'],$data['geye_fee']);
                $re_name = ['sxfsj_edit'];
                $re_value = ['1'];
                $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
                returnSuccess([],"操作成功");
            }else {
                returnError("操作失败");
            }
		}else{
		    $shuju = array(
		        'fee_zu_id'       =>$data['fee_zu_id'],
		        'heyue_zu_id'     =>$data['heyue_zu_id'],
		        'jian_yongjin'    =>$data['jian_yongjin'],
		        'ping_jin_yongjin'=>$data['ping_jin_yongjin'],
		        'ping_zuo_yongjin'=>$data['ping_zuo_yongjin'],
		        'geye_fee'        =>$data['geye_fee']
		    );
            $info = db('fee_yonghu_zu_detail')->insert($shuju);
            if ($info){
                returnSuccess([],"操作成功");
            }else {
                returnError("操作失败");
            }
        }
	}
	/************手续费模板结束***********/
	
	/************成本模板开始***********/
	
	//加载成本模板
	public function cbmb_left()
	{
		$result = db('fee_jujian_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}
	
	// 添加成本模板
	public function cbmb_tjmb()
	{
	    $data['name'] = input('name',"","trim");
		$data['beizhu'] = input('beizhu',""."trim");
		if (!$data['name'] || !$data['beizhu']) returnError("请完整填写参数");
		$cb_all = db('fee_jujian_zu')->where('name',$data['name'])->where("bz",1)->count();
        if ($cb_all>0) returnError("模板名称已被使用");
        $data['bz'] = 1;
        $data['ib_id'] = $this->gt_jujian_id;
		$result = db('fee_jujian_zu')->insertGetId($data);
        if($result>0){
            $operation_category = 42;
            $detail_name = array('添加成本模板','成本模板ID','成本模板名称','成本模板说明');
            $operation_value = array('成功',$result,$data['name'],$data['beizhu']);
            $re_name = ['cbmb_add'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 删除成本模板
	public function cbmb_scmb()
	{
		$data['id']   = input('id/d');
		if (!$data['id']) returnError("参数错误");
        $judge = db('jujian')
                ->where('fee_zu_id',$data['id'])
                ->count();
        $res = array();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('fee_jujian_zu')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                $this->riZhi(105,['删除成本模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>0],['update_bz'=>'1']);
                returnSuccess([],'操作成功');
            }else{
                returnError("操作失败");
            }
        }
	}
	
	// 加载成本模板数据
	public function cbmb_jzsj()
	{
	    $data['id'] = input('id/d');
	    if (!$data['id']) returnError("参数错误");
		$res = array();
		$heyue_new = db('heyue_zu')->field('id,bh,name')->select();
		$result_fee_jujian_zu_detail = db('fee_jujian_zu_detail')->where('fee_zu_id',$data['id'])->select();
		if(!empty($result_fee_jujian_zu_detail)){
			//两联表查询
			$res = db('fee_jujian_zu_detail')
            ->alias('de')
            ->join('heyue_zu he','de.heyue_zu_id = he.id')
			->where('de.fee_zu_id',$data['id'])
			->field('de.heyue_zu_id,de.jian_yongjin,de.ping_zuo_yongjin,de.ping_jin_yongjin,de.geye_fee,de.toucun_bili,he.name,he.bh,he.id')
            ->order('id')
            ->select();
			//遍历出来表中的heyue_id
			$detail_number = array();
			foreach($res as $x=>$val)
              {
                $detail_number[$x] = $val['heyue_zu_id'];
              }
			//查出不存在fee_jujian_zu_detail表中的数据
			$heyue_new = db('heyue_zu')
			->where('id','not in',$detail_number)
			->field('id,bh,name')
			->select();
		}
		foreach($heyue_new as $x=>$val)
		{
		    $heyue_new[$x]['jian_yongjin'] = 0;
		    $heyue_new[$x]['ping_zuo_yongjin'] = 0;
		    $heyue_new[$x]['ping_jin_yongjin'] = 0;
		    $heyue_new[$x]['geye_fee'] = 0;
		    $heyue_new[$x]['toucun_bili'] = 0;
		}
		$return = array_merge($res,$heyue_new);
		returnSuccess($return);
	}

	// 编辑成本模板
    public function cbmb_xgcbmb()
    {
        $data['id'] = input('id/d');
        $data['name'] = input("name","","trim");
        $data['beizhu'] = input("beizhu","","trim");
        if (!$data['id'] || !$data['name'] || !$data['beizhu']) returnError("参数错误");
        $res = db("fee_jujian_zu")->find($data['id']);
        if ($res){
            if ($data['name'] != $res['name'] && db("fee_jujian_zu")->where("name",$data['name'])->where("bz",1)->find()) returnError("模板名称已存在");
            if ($data['name'] != $res['name'] || $data['beizhu'] != $res['beizhu']){
                $result = db('fee_jujian_zu')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
                if($result>0){
                    $operation_category = 43;
                    $detail_name = array('编辑成本模板','成本模板ID','成本模板名称','成本模板说明');
                    $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
                    $re_name = ['cbmb_edit'];
                    $re_value = ['1'];
                    $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
                    returnSuccess([],"操作成功");
                }else{
                    returnError("操作失败");
                }
            }else {
                returnSuccess([],"操作成功");
            }
        }else {
            returnError("操作失败");
        }
    }
    
    // 获取当前成本模板信息
    public function sccbsj(){
        $data['id'] = input('id/d');
        $data['moban_id'] = input("moban_id/d");
        if (!$data['id'] || !$data['moban_id']) returnError("参数错误");
        $row = Db::name('fee_jujian_zu_detail')
            ->alias('f')
            ->field('h.id,h.name,h.bh,f.id as fee_jujian_zu_detail_id,f.jian_yongjin,f.ping_zuo_yongjin,f.ping_jin_yongjin,f.toucun_bili')
            ->join('heyue_zu h','f.heyue_zu_id = h.id')
            ->where('h.id', $data['id'])
            ->where('f.fee_zu_id', $data['moban_id'])
            ->find();
        returnSuccess($row);
    }
    
    // 修改成本模板信息
    public function tjxgcbsj(){
        $data['heyue_zu_id'] = input('id/d');
        $data['fee_zu_id'] = input('fee_zu_id/d');
        $data['geye_fee'] = input("toucun_bili",'','trim');
        $data['jian_yongjin'] = input('jian_yongjin','','trim');
        $data['ping_jin_yongjin'] = input('ping_jin_yongjin','','trim');
        $data['ping_zuo_yongjin'] = input('ping_zuo_yongjin','','trim');
        if (!$data['heyue_zu_id'] || !$data['fee_zu_id']) returnError("缺少重要参数");
        if (!is_numeric($data['geye_fee']) || !is_numeric($data['jian_yongjin']) || !is_numeric($data['ping_jin_yongjin']) || !is_numeric($data['ping_zuo_yongjin'])) returnError("数据格式错误");
        $have = db('fee_jujian_zu_detail')
        ->where('heyue_zu_id',$data['heyue_zu_id'])
        ->where('fee_zu_id',$data['fee_zu_id'])
        ->count();
        if ($have>0){
            // 最小限制
            $minInfo = db("jujian")
            ->alias("y")
            ->join("jujian j","y.par_id=j.id and j.fee_zu_id!=y.fee_zu_id and j.lb_id != 1","LEFT")
            ->join("toucun_zu t","t.id=j.toucun_zu_id","LEFT")
            ->join("fee_jujian_zu_detail fd","j.fee_zu_id=fd.fee_zu_id and fd.heyue_zu_id={$data['heyue_zu_id']}")
            ->where('y.fee_zu_id',$data['fee_zu_id'])
            ->where('t.fanyong_lb != 4')
            ->field("max(fd.jian_yongjin) as jian_yongjin,max(fd.ping_jin_yongjin) as ping_jin_yongjin,max(fd.ping_zuo_yongjin) as ping_zuo_yongjin")
            ->find();
            if (!$minInfo){
                $minInfo['jian_yongjin'] = 0;
                $minInfo['ping_jin_yongjin'] = 0;
                $minInfo['ping_zuo_yongjin'] = 0;
            }
            // 最大限制
            // 居间限制
            $maxJjInfo = db("jujian")
            ->alias("y")
            ->join("jujian j","y.id=j.par_id and j.fee_zu_id!=y.fee_zu_id","LEFT")
            ->join("fee_jujian_zu_detail fd","j.fee_zu_id=fd.fee_zu_id and fd.heyue_zu_id={$data['heyue_zu_id']}")
            ->where('y.fee_zu_id',$data['fee_zu_id'])
            ->field("min(fd.jian_yongjin) as jian_yongjin,min(fd.ping_jin_yongjin) as ping_jin_yongjin,min(fd.ping_zuo_yongjin) as ping_zuo_yongjin")
            ->find();
            // 用户限制
            $maxYhInfo = db("yonghu")
            ->alias("y")
            ->join("jujian j","y.jujian_id=j.id and j.fee_zu_id!=y.fee_zu_id","LEFT")
            ->join("fee_jujian_zu_detail fd","j.fee_zu_id=fd.fee_zu_id and fd.heyue_zu_id={$data['heyue_zu_id']}")
            ->where('y.fee_zu_id',$data['fee_zu_id'])
            ->field("min(fd.jian_yongjin) as jian_yongjin,min(fd.ping_jin_yongjin) as ping_jin_yongjin,min(fd.ping_zuo_yongjin) as ping_zuo_yongjin")
            ->find();
            $maxInfo['jian_yongjin'] = min($maxJjInfo['jian_yongjin'],$maxYhInfo['jian_yongjin']);
            $maxInfo['ping_jin_yongjin'] = min($maxJjInfo['ping_jin_yongjin'],$maxYhInfo['ping_jin_yongjin']);
            $maxInfo['ping_zuo_yongjin'] = min($maxJjInfo['ping_zuo_yongjin'],$maxYhInfo['ping_zuo_yongjin']);
            if ($data['jian_yongjin'] >= 0){
                if ($maxInfo['jian_yongjin'] == 0){
                    if ($data['jian_yongjin'] < $minInfo['jian_yongjin']){
                        returnError("开仓手续费必须大于({$minInfo['jian_yongjin']})");
                    }
                }else {
                    if ($data['jian_yongjin'] < $minInfo['jian_yongjin'] || $data['jian_yongjin'] > $maxInfo['jian_yongjin']){
                        returnError("开仓手续费必须在({$minInfo['jian_yongjin']} ~ {$maxInfo['jian_yongjin']})之间");
                    }
                }
            }else {
                returnError("开仓手续费不能小于0");
            }
            if ($data['ping_jin_yongjin'] >= 0){
                if ($maxInfo['ping_jin_yongjin'] == 0){
                    if ($data['ping_jin_yongjin'] < $minInfo['ping_jin_yongjin']){
                        returnError("平今手续费必须大于({$minInfo['ping_jin_yongjin']})");
                    }
                }else {
                    if ($data['ping_jin_yongjin'] < $minInfo['ping_jin_yongjin'] || $data['ping_jin_yongjin'] > $maxInfo['ping_jin_yongjin']){
                        returnError("平今手续费必须在({$minInfo['ping_jin_yongjin']} ~ {$maxInfo['ping_jin_yongjin']})之间");
                    }
                }
            }else {
                returnError("平今手续费不能小于0");
            }
            if ($data['ping_zuo_yongjin'] >= 0){
                if ($maxInfo['ping_zuo_yongjin'] == 0){
                    if ($data['ping_zuo_yongjin'] < $minInfo['ping_zuo_yongjin']){
                        returnError("平左手续费必须大于({$minInfo['ping_zuo_yongjin']})");
                    }
                }else {
                    if ($data['ping_zuo_yongjin'] < $minInfo['ping_zuo_yongjin'] || $data['ping_zuo_yongjin'] > $maxInfo['ping_zuo_yongjin']){
                        returnError("平左手续费必须在({$minInfo['ping_zuo_yongjin']} ~ {$maxInfo['ping_zuo_yongjin']})之间");
                    }
                }
            }else {
                returnError("平左手续费不能小于0");
            }
            $res = Db::name('fee_jujian_zu_detail')
            ->where('heyue_zu_id',$data['heyue_zu_id'])
            ->where('fee_zu_id',$data['fee_zu_id'])->update($data);
        }else {
            $res = Db::name('fee_jujian_zu_detail')->insert($data);
        }
        if($res){
            $operation_category = 105;
            $detail_name = array('编辑成本项目','成本模板ID','合约全局ID','开仓','平今','平仓','手续费');
            $operation_value = array('成功',$data['fee_zu_id'],$data['heyue_zu_id'],$data['jian_yongjin'],$data['ping_jin_yongjin'],$data['ping_zuo_yongjin'],$data['geye_fee']);
            $re_name = ['cbmbsj_edit'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess([],"操作成功");
        }else {
            returnError("操作失败");
        }
    }
    /************成本模板结束***********/
	
    /************保证金模板开始***********/
    // 查询保证金模板
	public function bzjmb_left()
	{
		$result = db('baozheng_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}
	
	// 添加保证金模板
	public function bzjmb_tjmb()
	{
		$insert['name'] = input('name',"","trim");
		if (!$insert['name']) returnError("模板名称不能为空");
		$insert['beizhu'] = input("beizhu","","trim");
		$insert['ib_id'] = $this->gt_jujian_id;
		$insert['bz'] = 1;
        $bz_all = db('baozheng_zu')
        ->where('name',$insert['name'])
        ->where("bz",1)
        ->count();
        if ($bz_all>0) returnError("此模板名称已被使用"); 
        $result = db('baozheng_zu')->insertGetId($insert);
        if($result>0){
            $operation_category = 44;
            $detail_name = array('添加保证金模板','保证金模板ID','保证金模板名称','保证金模板说明');
            $operation_value = array('成功',$result,$insert['name'],$insert['beizhu']);
            $re_name = ['bzjmb_add'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 删除保证金模板
	public function bzjmb_scmb()
	{
		$data['id']   = input("id",0,'intval');
		if (!$data['id']) returnError("缺少重要参数");
        $judge = db('yonghu')
            ->where('baozheng_zu_id',$data['id'])
            ->count();
        $res = array();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('baozheng_zu')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                returnSuccess([],'操作成功');
            }else{
                returnError("操作失败");
            }
        }
	}
	
	// 加载保证金模板数据
	public function bzjmb_jzsj()
	{
		$data['id'] = input('id',0,"trim");
		if (!$data['id']) returnError("参数错误");
		$res = array();
		$heyue_new = db('heyue_zu')->field('id as heid,bh as hebh,name as hename')->select();
        $result_baozheng_zu_detail = db('baozheng_zu_detail')->where('baozheng_zu_id',$data['id'])->select();
        if(!empty($result_baozheng_zu_detail)){
            //两联表查询
            $res = db('baozheng_zu_detail')
                ->alias('de')
                ->join('heyue_zu he','de.heyue_zu_id = he.id')
                ->where('de.baozheng_zu_id',$data['id'])
                ->field('de.*,he.name as hename,he.bh as hebh,he.id as heid')
                ->select();
            //遍历出来表中的heyue_id
            $detail_number = array();
            foreach($res as $x=>$val)
            {
                $detail_number[$x] = $val['heyue_zu_id'];
            }
            //查出不存在fee_jujian_zu_detail表中的数据
            $heyue_new = db('heyue_zu')
                ->where('id','not in',$detail_number)
                ->field('id as heid,bh as hebh,name as hename')
                ->select();
        }
        foreach($heyue_new as $x=>$val)
        {
            $heyue_new[$x]['jiancang_qishi_jine'] = 0;
            $heyue_new[$x]['jiancang_qishi_num'] = 0;
            $heyue_new[$x]['jiancang_jiange_jine'] = 0;
            $heyue_new[$x]['jiancang_jiange_num'] = 0;
            $heyue_new[$x]['baojing_jine'] = 0;
            $heyue_new[$x]['ping_jine'] = 0;
            $heyue_new[$x]['geye_jine'] = 0;
        }
        $result_baozheng_zu_detail = array_merge($res,$heyue_new);
        returnSuccess($result_baozheng_zu_detail);
	}

	// 修改保证金模板
	public function bzjmb_xgbzjmb()
	{
	    $data['id'] = input('id',0,'intval');
		$data['name'] = input('name','','trim');
		$data['beizhu'] = input('beizhu','','trim');
		if (!$data['id']) returnError("参数错误");
		if (!$data['name']) returnError("模板名称不能为空");
		$res = db('baozheng_zu')->find($data['id']);
		if ($res){
		    if ($data['name'] != $res['name'] && db('baozheng_zu')->where("name",$data['name'])->where("bz",1)->find()) returnError("此模板名称已存在");
		    if ($data['name'] != $res['name'] || $data['beizhu'] != $res['beizhu']){
		        $result = db('baozheng_zu')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		        if($result>0){
		            $operation_category = 45;
		            $detail_name = array('编辑保证金模板','保证金模板ID','保证金模板名称','保证金模板说明');
		            $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
		            $re_name = ['bzjmb_edit'];
		            $re_value = ['1'];
		            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
		            returnSuccess(1);
		        }else{
		            returnError("编辑保证金模板失败");
		        }
		    }else {
		        returnSuccess(1);
		    }
		}else {
		    returnError("未找到相应模板");
		}
	}
	
	//
	public function scbzjsj(){
		$data = input();
		$result_detail = db('baozheng_zu_detail')
            ->where('heyue_zu_id',$data['heyue_zu_id'])
            ->where('baozheng_zu_id',$data['baozheng_zu_id'])
            ->select();
		if(empty($result_detail)){
			return 1;
		}else{
			$result_heyue_zu_name = db('heyue_zu')->where('id',$data['heyue_zu_id'])->value('name');
			$result_heyue_zu_bh = db('heyue_zu')->where('id',$data['heyue_zu_id'])->value('bh');
			$result = array($result_detail,$result_heyue_zu_name,$result_heyue_zu_bh);
			return $result;
		}
	}
	
	// 修改保证金模板数据
	public function tjxgbzjsj(){
	    $data['id'] = input('id/d');
	    $data['heyue_zu_id'] = input('heyue_zu_id/d');
	    $data['baozheng_zu_id'] = input('baozheng_zu_id/d');
	    $data['geye_jine'] = input('geye_jine','','trim');
	    $data['ping_jine'] = input('ping_jine','','trim');
	    $data['baojing_jine'] = input('baojing_jine','','trim');
	    $data['jiancang_qishi_num'] = input('jiancang_qishi_num','','trim');
	    $data['jiancang_qishi_jine'] = input('jiancang_qishi_jine','','trim');
	    $data['jiancang_jiange_num'] = input('jiancang_jiange_num','','trim');
	    $data['jiancang_jiange_jine'] = input('jiancang_jiange_jine','','trim');
		if (!isset($data['heyue_zu_id']) || !isset($data['baozheng_zu_id'])) returnError("缺少重要参数");
		if (!is_numeric($data['geye_jine']) || !is_numeric($data['ping_jine']) || !is_numeric($data['baojing_jine']) || !is_numeric($data['jiancang_qishi_num']) || !is_numeric($data['jiancang_qishi_jine']) || !is_numeric($data['jiancang_jiange_num']) || !is_numeric($data['jiancang_jiange_jine'])) returnError("数据格式错误");
        $result_detail = db('baozheng_zu_detail')
            ->where('heyue_zu_id',$data['heyue_zu_id'])
            ->where('baozheng_zu_id',$data['baozheng_zu_id'])
            ->select();
        if(empty($result_detail)){
            if (isset($data['id'])) unset($data['id']);
            $result = db('baozheng_zu_detail')
                ->insertGetId($data);
        }else{
            $result = db('baozheng_zu_detail')
                ->where('heyue_zu_id',$data['heyue_zu_id'])
                ->where('baozheng_zu_id',$data['baozheng_zu_id'])
                ->update(['jiancang_qishi_jine'=>$data['jiancang_qishi_jine'],'jiancang_qishi_num'=>$data['jiancang_qishi_num'],'jiancang_jiange_jine'=>$data['jiancang_jiange_jine'],'jiancang_jiange_num'=>$data['jiancang_jiange_num'],'baojing_jine'=>$data['baojing_jine'],'ping_jine'=>$data['ping_jine'],'geye_jine'=>$data['geye_jine']]);
        }
        if($result>0){
            $operation_category = 117;
            $detail_name = array('编辑保证金项目','保证金模板ID','合约全局ID','起始金额','合约手数限制','金额递增','合约递增手数限制','报警金额','爆仓金额','隔夜费保证金');
            $operation_value = array('成功',$data['baozheng_zu_id'],$data['heyue_zu_id'],$data['jiancang_qishi_jine'],$data['jiancang_qishi_num'],$data['jiancang_jiange_jine'],$data['jiancang_jiange_num'],$data['baojing_jine'],$data['ping_jine'],$data['geye_jine']);
            $re_name = ['xgbzjsj_edit'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	/************保证金模板结束***********/
	
	/************风控模板开始***********/
	//风控模板开始
	public function cxfkmb_left()
	{
		$result = db('fengkong_moban')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}
	
	// 添加风控模板
	public function fkmb_tjmb(){
		$data['name'] = input('name','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		$data['beizhu'] = input('beizhu','','trim');
		$data['ib_id'] = $this->gt_jujian_id;
		$data['bz'] = 1;
        $fk_all = db('fengkong_moban')
            ->where('name',$data['name'])
            ->where("bz",1)
            ->count();
        if ($fk_all>0){
            returnError("此模板名称已被使用");
        }else {
    		$result = db('fengkong_moban')->insertGetId($data);
            if($result>0){
                $operation_category = 46;
                $detail_name = array('添加风控模板','风控模板ID','风控模板名称','风控模板说明');
                $operation_value = array('成功',$result,$data['name'],$data['beizhu']);
                $re_name = ['fkmb_add'];
                $re_value = ['1'];
                $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
                returnSuccess(1);
            }else{
                returnError("添加风控模板失败");
            }
        }
	}
	
	// 删除风控模板
    public function fkmb_scmb()
    {
        $data['id'] = input('id',0,'intval');
        if (!$data['id']) returnError("参数错误，操作失败");
        $judge = db('yonghu')
            ->where('fengkong_zu_id',$data['id'])
            ->count();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('fengkong_moban')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                returnSuccess(1);
            }else{
                returnError("删除风控模板失败");
            }
        }
    }
    
    // 确认修改模板
	public function FK_qrxgmb(){
		$data['id'] = input('id',0,'intval');
		$data['name'] = input("name","","trim");
		if (!$data['name']) returnError("模板名称不能为空");
		$data['beizhu'] = input('beizhu','','trim');
		$res = db('fengkong_moban')->find($data['id']);
		if ($res){
		    if ($data['name'] != $res['name'] && db('fengkong_moban')->where("name",$data['name'])->where("bz",1)->find()) returnError("此模板名称已存在");
		    if ($data['name'] != $res['name'] || $data['beizhu'] != $data['beizhu']) {
		        $result = db('fengkong_moban')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		        if($result>0){
		            $operation_category = 47;
		            $detail_name = array('编辑风控模板','风控模板ID','风控模板名称','风控模板说明');
		            $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
		            $re_name = ['fkmb_edit'];
		            $re_value = ['1'];
		            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
		            returnSuccess(1);
		        }else{
		            returnError("操作失败");
		        }
		    }else {
		        returnError(1);
		    }
		}else {
		    returnError("操作失败");
		}
	}
	
	// 风控模板规则
	public function cxktjx()
	{
		$result = db('fengkong_rule')->select();
		returnSuccess($result);
	}
	
	// 加载风控模板
	public function fkmb_jzsj(){
		$data['id'] = input('id',0,'intval');
		if (!$data['id']) returnError("缺少参数");
		$result = db('fengkong_moban_detail')
		->alias('f')
		->join('fengkong_rule r','f.rule_id=r.id')
		->where('moban_id',$data['id'])
		->field("f.id,f.moban_id,f.zhi_int_value,f.zhi_float_value,f.zhi_shijian_value,f.zhi_shijian1_value,f.heyue_zu_bh,r.value_lb,r.name")
		->select();
		if(!$result) returnError("暂无数据");
		foreach ($result as $k => $v){
		    if ($v['value_lb'] == 1){
		        $result[$k]['content'] = $v['zhi_float_value'];
		    }elseif ($v['value_lb'] == 2){
		        $result[$k]['content'] = $v['zhi_int_value'];
		    }elseif ($v['value_lb'] == 3){
		        $result[$k]['content'] = $v['zhi_int_value']?"是":"否";
		    }elseif ($v['value_lb'] == 4){
		        $result[$k]['content'] = date("Y-m-d H:i:s",$v['zhi_shijian_value'])." 至 ".date("Y-m-d H:i:s",$v['zhi_shijian1_value']);
		    }
		}
		returnSuccess($result);
	}
	
	// 获取规则值
	public function fkmb_tjsj()
	{
		$data['id'] = input('id',0,'intval');
		if (!$data['id']) returnError("缺少参数");
		$result = db('fengkong_rule')->where('id',$data['id'])->value('value_lb'); 
        returnSuccess($result);
	}
	
	// 执行添加
	public function FK_float_tj()
	{
	    $value_lb = input('value_lb',0,'intval');
	    if ($value_lb == 1){ // 填写值 接收zhi_float_value
    		$data['zhi_float_value'] = input('zhi_float_value','','trim');
    		if (!$data['zhi_float_value']) returnError("参数错误");
    		if (!is_numeric($data['zhi_float_value'])) returnError("请填写正确参考值");
	    }elseif ($value_lb == 2){ // 填写值 接收zhi_int_value
	        $data['zhi_int_value'] = input("zhi_int_value",'','trim');
	        if (!$data['zhi_int_value']) returnError("参数错误");
	        if (!is_numeric($data['zhi_int_value'])) returnError("请填写正确参考值");
	    }elseif ($value_lb == 3){ // 选项 1是 0否
	        $data['zhi_int_value'] = input('zhi_int_value','','trim');
	        if (!is_numeric($data['zhi_int_value'])) returnError("参数错误");
	    }elseif ($value_lb == 4){ // 时间
	        $data['zhi_shijian_value'] = input('zhi_shijian_value');
	        $data['zhi_shijian1_value'] = input('zhi_shijian1_value');
	        if (!$data['zhi_shijian_value'] || !$data['zhi_shijian1_value']) returnError("参数错误");
	        $data['zhi_shijian_value'] = strtotime($data['zhi_shijian_value']);
	        $data['zhi_shijian1_value'] = strtotime($data['zhi_shijian1_value']);
	        if ($data['zhi_shijian1_value'] < $data['zhi_shijian_value']) returnError("请正确填写起止时间");
	    }else {
	        returnError("参数错误");
	    }
		$data['rule_id'] = input('rule_id/d');
		$data['moban_id'] = input("moban_id/d");
		$data['heyue_zu_bh'] = input('heyue_zu_bh',"","trim");
		$result = db('fengkong_moban_detail')->insert($data);
        if($result>0){
            $operation_category = 48;
            $detail_name = array('添加风控项目','操作者ID');
            $operation_value = array('成功',$this->id);
            $re_name = ['fkxm_add'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	
	// 删除数据
	public function scsj(){
		$data['id'] = input('id',0,'intval');
		if (!$data['id']) returnError("参数错误,操作失败");
		$result = db('fengkong_moban_detail')->where('id',$data['id'])->delete();
		if($result>0){
            $operation_category = 49;
            $detail_name = array('删除风控项目','风控项目ID','操作者ID');
            $operation_value = array('成功',$data['id'],$this->id);
            $re_name = ['fkxm_del'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	/***********风控模板结束**********/
	
	/***********汇率模板开始**********/
	//汇率模板开始
	public function hlmb_left()
	{
		$result = db('huobi_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}
	
	// 添加汇率模板
	public function hlmb_tjmb()
	{
		$data['name']   = input('name','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		$data['beizhu'] = input('beizhu','','trim');
        $hl_all = db('huobi_zu')
            ->where('name',$data['name'])
            ->where("bz",1)
            ->count();
        if ($hl_all>0) returnError("此模板名称已被使用");
		$shuju = ['ib_id'=>$this->gt_jujian_id,'name'=>$data['name'],'beizhu'=>$data['beizhu'],'bz'=>1];
		$result = db('huobi_zu')->insert($shuju);
		if($result){
            $operation_category = 50;
            $detail_name = array('添加汇率模板','汇率模板名称','汇率模板备注');
            $operation_value = array('成功',$data['name'],$data['beizhu']);
            $re_name = ['hlmb_add'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	
	// 删除模板
	public function hlmb_scmb()
	{
		$data['id']   = input('id/d');
		if (!$data['id']) returnError("参数错误");
        $judge = db('yonghu')
            ->where('huobi_zu_id',$data['id'])
            ->count();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $judge_jujian =  db('jujian')
                ->where('hb_zu_id',$data['id'])
                ->count();
            if($judge_jujian>0){
                returnError("此模板正在使用中，无法删除");
            }else {
                $result = db('huobi_zu')
                    ->where('id',$data['id'])
                    ->update(['bz'=>0]);
                if($result){
                    $this->riZhi(119,['删除汇率模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>0],['update_bz'=>'1']);
                    returnSuccess(1);
                }else{
                    returnError("删除汇率模板失败");
                }
            }
        }
	}
	
	// 加载汇率数据
	public function hlmb_jzsj()
	{
		$data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
		$res = array();
		$heyue_new = db('huobi')->field('id,name')->select();
        $result_huobi_zu_detail = db('huobi_zu_detail')->where('hb_zu_id',$data['id'])->select();
        if(!empty($result_huobi_zu_detail)){
            //两联表查询
            $res = db('huobi_zu_detail')
                ->alias('de')
                ->join('huobi hb','de.hb_id = hb.id')
                ->where('de.hb_zu_id',$data['id'])
                ->field('de.bilv,de.shouxu_fee,hb.name,hb.id')
                ->select();
            //遍历出来表中的hb_id
            $detail_number = array();
            foreach($res as $x=>$val)
            {
                $detail_number[$x] = $val['id'];
            }
            //查出不存在huobi_zu_detail表中的数据
            $heyue_new = db('huobi')
                ->where('id','not in',$detail_number)
                ->field('id,name')
                ->select();
        }
        foreach($heyue_new as $x=>$val)
        {
            $heyue_new[$x]['bilv'] = 0;
            $heyue_new[$x]['shouxu_fee'] = 0;

        }
        $result = array_merge($res,$heyue_new);
        returnSuccess($result);
	}
	
	// 修改汇率模板
	public function hlmb_xghlmb()
	{
		$data['id'] = input("id/d");
		$data['name'] = input('name','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		$data['beizhu'] = input('beizhu','','trim');
		$res = db('huobi_zu')->find($data['id']);
		if ($res){
		    if ($data['name'] != $res['name'] && db('huobi_zu')->where("name",$data['name'])->where("bz",1)->find()) returnError("此模板名称已存在");
		    if ($data['name'] != $res['name'] || $data['beizhu'] != $res['beizhu']){
		        $result = db('huobi_zu')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		        if($result){
		            $operation_category = 51;
		            $detail_name = array('编辑汇率模板','汇率模板ID','汇率模板名称','汇率模板备注');
		            $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
		            $re_name = ['hlmb_edit'];
		            $re_value = ['1'];
		            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
		            returnSuccess(1);
		        }else {
		            returnError("操作失败");
		        }
		    }else {
		        returnSuccess(1);
		    }
		}else {
		    returnError("操作失败");
		}
	}
	
	public function schlsj(){
		$data['id'] = input('id/d');
		$data['moban_id'] = input('moban_id/d');
		$result_detail = db('huobi_zu_detail')->where('hb_id',$data['id'])->where('hb_zu_id',$data['moban_id'])->select();
		returnSuccess($result_detail);
	}
	
	// 修改汇率数据
	public function tjxghlsj(){
		$data['hb_id'] = input("hb_id/d");
		$data['hb_zu_id'] = input('hb_zu_id/d');
		$data['bilv'] = input('bilv','','trim');
		$data['shouxu_fee'] = input('shouxu_fee','','trim');
		if (!$data['hb_id'] || !$data['hb_zu_id']) returnError("参数错误");
		if (!is_numeric($data['bilv']) || !is_numeric($data['shouxu_fee'])) returnError("数据格式错误");
        $result_detail = db('huobi_zu_detail')
            ->where('hb_id',$data['hb_id'])
            ->where('hb_zu_id',$data['hb_zu_id'])
            ->find();
        if($result_detail){
            $result = db('huobi_zu_detail')
                ->where('hb_id',$data['hb_id'])
                ->where('hb_zu_id',$data['hb_zu_id'])
                ->update($data);
        }else{
            $result = db('huobi_zu_detail')->insertGetId($data);
        }
        if($result>0){
            $operation_category = 119;
            $detail_name = array('编辑汇率项目','汇率模板ID','货币ID','汇率','手续费');
            $operation_value = array('成功',$data['hb_zu_id'],$data['hb_id'],$data['bilv'],$data['shouxu_fee']);
            $re_name = ['xghlsj_edit'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	/**************汇率模板结束***********/
	
	
	
	//权限模板开始
	public function qxmb_left()
	{
		$result = db('jujian_lb') 
		->where('jujian_id',($this->isCZY)?$this->par_id:$this->id)
        ->where('bz',1)
        ->field('id,name,qx')
        ->select();
		$caozuo = db("jujian_caozuo")->select();
		returnSuccess([$result,$caozuo]);
	}	
	
	// 添加权限模板 获取当前用户权限
	public function qxmb_tjmb()
	{
	    //获取自己的的权限数字序列
        $ziji_quanxian = db('jujian')
                ->join('jujian_lb','jujian.lb_id=jujian_lb.id','RIGHT')
                ->where('jujian.id',($this->isCZY)?$this->par_id:$this->id)
                ->value('jujian_lb.qx');
        $ziji_quanxian = explode(',',$ziji_quanxian);
		$result_jujian_caozuo = db('jujian_caozuo')->select();
		foreach ($result_jujian_caozuo as $k => $v){
		    $result_jujian_caozuo[$k]['is_show'] = $ziji_quanxian[$k];
		}
		returnSuccess($result_jujian_caozuo);
	}
	
	// 删除权限模板
	public function qxmb_scmb()
	{
	    $data['id'] = input('id/d');
	    if (!$data['id']) returnError("参数错误");
        $judge = db('jujian')
            ->where('lb_id',$data['id'])
            ->count();
        $res = array();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('jujian_lb')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                $this->riZhi(53,['删除权限模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>0],['update_bz'=>'1']);
                returnSuccess(1);
            }else{
                returnError("删除权限模板失败");
            }
        }
	}
	
	// 加载数据
	public function qxmb_jzsj()
	{
		$data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
		$result_jujian_lb = db('jujian_lb')->find($data['id']);
		$result_jujian_caozuo = db('jujian_caozuo')->field('name')->select();
		$quanxian = explode(",",$result_jujian_lb['qx']);
		$array = array();
		for($i=0;$i<count($quanxian);$i++){		
			if($quanxian[$i]==1){
				$array[] = $result_jujian_caozuo[$i]['name'];
			}		
		}
		$qx = implode(",",$array);
		$result_jujian_lb['qx'] = $qx;
		returnSuccess($result_jujian_lb);
	}
	
	// 确认添加权限
	public function qrtjqx()
	{
	    $data['name'] = input('name','','trim');
		if (!$data['name']) returnError("名称不能为空");
		if (db('jujian_lb')->where("name",$data['name'])->where("bz",1)->find()) returnError("此模板名称已存在");
		$data['jujian_id'] = ($this->isCZY)?$this->par_id:$this->id;
		$data['bz'] = 1;
		$qxArray = input('qx/a');
		$ids = $qx = array();
		foreach ($qxArray as $k => $v){
		    array_push($ids, $v['id']);
		}
		array_multisort($ids,SORT_ASC,$qxArray);
		foreach ($qxArray as $k => $v){
		    array_push($qx, $v['check']);
		}
		$data['qx'] = trim(implode(',',$qx));
		$result = db('jujian_lb')->insertGetId($data);
        if($result>0){
            $operation_category = 52;
            $detail_name = array('添加权限模板','权限模板ID','权限模板名称');
            $operation_value = array('成功',$result,$data['name']);
            $re_name = ['qxmb_add'];
            $re_value = ['1'];
            $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            returnSuccess(1);
        }else {
            returnError("操作失败");
        }
	}
	
	// 修改权限模板
	public function qxmb_xgqxmb()
	{
		$id = input("id/d");
		if (!$id) returnError("参数错误");
		$return = db('jujian_lb')->field("id,name,qx")->find($id);
		returnSuccess($return);
	}
	
	// 确认修改权限模板
	public function qxmb_qrxg()
	{
	    $data['id'] = input('id/d');
	    if (!$data['id']) returnError("参数错误");
	    $data['name'] = input('name','','trim');
	    if (!$data['name']) returnError("名称不能为空");
	    $res = db('jujian_lb')->find($data['id']);
	    if (!$res) returnError("获取模板信息失败");
	    if ($res && $data['name'] != $res['name'] && db('jujian_lb')->where("name",$data['name'])->where("bz",1)->find()) returnError("此模板名称已存在");
	    $qxArray = input('qx/a');
	    $ids = $qx = array();
	    foreach ($qxArray as $k => $v){
	        array_push($ids, $v['id']);
	    }
	    array_multisort($ids,SORT_ASC,$qxArray);
	    foreach ($qxArray as $k => $v){
	        array_push($qx, $v['check']);
	    }
	    $data['qx'] = trim(implode(',',$qx));
	    if ($data['qx'] != $res['qx'] || $data['name'] != $res['name']){
    	    $result = db('jujian_lb')->update($data);
            if($result){
                $operation_category = 53;
                $detail_name = array('编辑权限模板','权限模板ID','权限模板名称');
                $operation_value = array('成功',$data['id'],$data['name']);
                $re_name = ['qxmb_edit'];
                $re_value = ['1'];
                $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
                returnSuccess(1);
            }else {
                returnError("操作失败");
            }
	    }else {
	        returnSuccess(1);
	    }
	}
	//权限模板结束

	//基础数据--头寸模板
	public function tcmb_cxmb(){
		$result = db('toucun_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->select();
		returnSuccess($result);
	}	
	
	// 删除头寸模板
	public function tcmb_scmb(){
		$data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
        $judge = db('jujian')
            ->where('toucun_zu_id',$data['id'])
            ->count();
        $res = array();
        if($judge>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('toucun_zu')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                $this->riZhi(53,['删除头寸模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>0],['update_bz'=>'1']);
                returnSuccess(1);
            }else{
                returnError("操作失败");
            }
        }
	}
	
	// 添加头寸模板
	public function tcmb_tjmb(){
	    $data['is_geye'] = input('is_geye/d');
	    $data['is_shouxu'] = input('is_shouxu/d');
	    $data['is_toucun'] = input('is_toucun/d');
	    $data['fanyong_lb'] = input('fanyong_lb/d');
	    $data['name'] = input('name','','trim');
	    $data['chengben'] = input('chengben','','trim');
	    $data['rujin_chengben'] = input('rujin_chengben','','trim');
	    $data['chujin_chengben'] = input('chujin_chengben','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		if (!is_numeric($data['chengben']) || !is_numeric($data['chujin_chengben']) || !is_numeric($data['rujin_chengben'])) returnError("数据格式错误");
        $tc_all = db('toucun_zu')
            ->where('name',$data['name'])
            ->where("bz",1)
            ->count();
        if ($tc_all>0){
            returnError("此模板名称已被使用");
        }else {
            $data['ib_id'] = $this->gt_jujian_id;
            $data['bz'] = 1;
            $result = db('toucun_zu')->insert($data);
            if ($result){
                $this->riZhi(104,['添加头寸模板'=>'成功','操作人员'=>$this->jujian['bh'],'bz'=>1],['update_bz'=>'1']);
                returnSuccess(1);
            }else {
                returnError("操作失败");
            }
        }
	}
	
	
	public function tcmb_xgmb_cr(){
		$data['id'] = input('id/d');
		if (!$data['id']) returnError("参数错误");
		$row = db('toucun_zu')->where('id',$data['id'])->find();
		returnSuccess($row);
	}
	
	// 修改头寸模板
	public function tcmb_xgmb(){
		$data['id'] = input('id/d');
		$data['is_geye'] = input('is_geye/d');
		$data['is_shouxu'] = input('is_shouxu/d');
		$data['is_toucun'] = input('is_toucun/d');
		$data['fanyong_lb'] = input('fanyong_lb/d');
		$data['name'] = input('name','','trim');
		$data['chengben'] = input('chengben','','trim');
		$data['rujin_chengben'] = input('rujin_chengben','','trim');
		$data['chujin_chengben'] = input('chujin_chengben','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		if (!is_numeric($data['chengben']) || !is_numeric($data['chujin_chengben']) || !is_numeric($data['rujin_chengben'])) returnError("数据格式错误");
		
		$is_geye = $data['is_geye'];
		$is_toucun = $data['is_toucun'];
		$is_shouxu = $data['is_shouxu'];
		$fanyong_lb = $data['fanyong_lb'];
		$oldInfo = db('toucun_zu')->where('id',$data['id'])->field('fanyong_lb,name')->find();
		if ($data['name'] != $oldInfo['name'] && db('toucun_zu')->where('name',$data['name'])->where('bz',1)->find()) returnError("此模板名称已被占用");
		$fanyong_old = $oldInfo['fanyong_lb'];
		$minInfo = db("jujian")
		->alias("y")
		->join("jujian j","y.par_id=j.id","LEFT")
		->join("toucun_zu t","j.toucun_zu_id=t.id and t.is_toucun=1")
		->where('y.toucun_zu_id',$data['id'])
		->where('y.bz=1 and y.lb_id=2')
		->field("min(t.chengben) as min")
		->find();
		if ($minInfo['min']){
    		if ($data['chengben'] > $minInfo['min']) {
    		    returnError("头寸占比不能大于上级({$minInfo['min']})");
    		}
		}
		$result = db('toucun_zu')
		->where('id',$data['id'])
		->update([
		    'name'            =>$data['name'],
		    'is_toucun'       =>$is_toucun,
		    'is_geye'         => $is_geye,
		    'chengben'        =>$data['chengben'],
		    'chujin_chengben' =>$data['chujin_chengben'],
		    'rujin_chengben'  =>$data['rujin_chengben'],
		    'is_shouxu'       =>$is_shouxu,
		    'fanyong_lb'      =>$fanyong_lb
		]);
		if ($result){
            if($fanyong_old != $fanyong_lb){
                $operation_category = 107;
                $detail_name = array('编辑返佣类别','头寸模板ID','返佣类别');
                $fanyong_name = db('fanyong_lb')
                    ->where('id',$fanyong_lb)
                    ->value('name');
                $operation_value = array('成功',$data['id'],$fanyong_name);
                $re_name = ['fanyonglb_edit'];
                $re_value = ['1'];
                $this->putRizhi($operation_category, $detail_name, $operation_value, $re_name, $re_value);
            }
            returnSuccess(1);
		}else {
		    returnError("操作失败");
		}
	}
	
	
	/************* 获取模板组信息start *******
	public function sxfmb_xgmb_crz(){ // 手续费
		$data = input('id','0','intval');
		if ($data == 0) returnError("参数错误");
		$result = db('fee_yonghu_zu')->where('id',$data)->find();
		returnSuccess($result);
	}
	public function cbmb_xgmb_crz(){ // 成本
	    $data = input('id','0','intval');
	    if ($data == 0) returnError("参数错误");
		$result = db('fee_jujian_zu')->where('id',$data)->find();
		returnSuccess($result);
	}
	public function bajmb_xgmb_crz(){ // 保证金
	    $data = input('id','0','intval');
	    if ($data == 0) returnError("参数错误");
		$result = db('baozheng_zu')->where('id',$data)->find();
		returnSuccess($result);
	}
	public function hlmb_xgmb_crz(){ // 货币
	    $data = input('id','0','intval');
	    if ($data == 0) returnError("参数错误");
		$result = db('huobi_zu')->where('id',$data)->find();
		returnSuccess($result);
	}
	***** 获取模板组信息end **************/
	
	
	//入金模板
	public function rjmb_left()
	{
		$result = db('rujin_zu')->where('ib_id',$this->gt_jujian_id)->select();
		returnSuccess($result);
	}
	
	
	// 加载数据
	public function rjmb_jzsj()
	{
	    $data['id'] = input('id/d');
	    if (!$data['id']) returnError("参数错误");
		$result = db('rujin_zu_detail')
		->alias('r')
		->join("churu_jiekou c","c.id=r.churu_id","LEFT")
		->where('rujin_zu_id',$data['id'])
		->field("c.name,r.id,r.wap_bz")
		->select();
		returnSuccess($result);
	}
	/**** 入金模板的操作
	
	// 入金模板添加
	public function rjmb_tjmb()
	{
		$data   = input();
		$shuju = ['ib_id'=>$data['ib_id'],'name'=>$data['name'],'moren_bz'=>1,'bz'=>1,'beizhu'=>$data['beizhu']];
		$result = db('rujin_zu')->insert($shuju);
		//halt($result);
		return $result;
	}
	public function rjmb_scmb(Request $request)
	{
		$data = request()->put();
		//halt($data);
 		$result = db('rujin_zu')->where('id',$data['id'])->delete();
 		return $result;
	}
	public function rjmb_xgmb_shuju(Request $request)
	{
		$data = request()->put();
		$result = db('rujin_zu')->where('id',$data['moban_id'])->select();
		return $result;
	}

	public function rjmb_xgmb(Request $request)
	{
		$data = request()->put();
		$result = db('rujin_zu')->where('id',$data['moban_id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		return $result;
	}


	public function rjmb_tjrjsj_jiekou()
	{
		$result = db('churu_jiekou')->where('churu_lb',1)->whereOr('churu_lb',2)->select();
		return $result;
	}

	public function rjmb_tjrjsj(Request $request)
	{
		$data = Request()->put();
		//halt($data);
		preg_match('/\d+/',$data['jiekou'],$jiekou);
		$jiekou_id = $jiekou[0];
		$shuju[] = ['rujin_zu_id'=>$data['moban_id'],'churu_id'=>$jiekou_id,'wap_bz'=>0];
		//halt($shuju);
		$result_detail = db('rujin_zu_detail')->insertAll($shuju);
		$result = array($result_detail,$data['moban_id']);
		return $result;
	}

	public function rjmb_xgrjsj_jiekou()
	{
		$result = db('churu_jiekou')->where('churu_lb',1)->whereOr('churu_lb',2)->select();
		return $result;
	}

	public function rjmb_xgrjsj(Request $request)
	{
		$data = Request()->put();
		//halt($data);
		preg_match('/\d+/',$data['jiekou'],$jiekou);
		$jiekou_id = $jiekou[0];
		//halt($jiekou_id);
		$result_detail = db('rujin_zu_detail')->where('id',$data['shuju_id'])->update(['churu_id'=>$jiekou_id]);
		$result = array($result_detail,$data['moban_id']);
		return $result;
	}
	
	 *****/
	//入金模板结束
	//出金模板
	public function cjmb_left()
	{
		$result = db('chujin_zu')->where('ib_id',$this->gt_jujian_id)->select();
		returnSuccess($result);
	}
	
	// 出金模板加载数据
	public function cjmb_jzsj()
	{
		$data['id'] = input("id/d");
		if (!$data['id']) returnError("参数错误");
		$result = db('chujin_zu_detail')
		->alias('r')
		->join("churu_jiekou c","c.id=r.churu_id","LEFT")
		->where('rujin_zu_id',$data['id'])
		->field("c.name,r.id,r.wap_bz")
		->select();
		returnSuccess($result);
	}
	
	/****** 出金模板的数据操作 
	 
	public function cjmb_tjmb(Request $request)
	{
		$data   = request()->put();
		$shuju[] = ['ib_id'=>$data['ib_id'],'name'=>$data['name'],'moren_bz'=>1,'bz'=>1,'beizhu'=>$data['beizhu']];
		$result = db('chujin_zu')->insertAll($shuju);
		//halt($result);
		return $result;
	}
	
	public function cjmb_scmb(Request $request)
	{
		$data = request()->put();
		//halt($data);
 		$result = db('chujin_zu')->where('id',$data['id'])->delete();
 		return $result;
	}
	public function cjmb_xgmb_shuju(Request $request)
	{
		$data = request()->put();
		$result = db('chujin_zu')->where('id',$data['moban_id'])->select();
		return $result;
	}

	public function cjmb_xgmb(Request $request)
	{
		$data = request()->put();
		$result = db('chujin_zu')->where('id',$data['moban_id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
		return $result;
	}


	public function cjmb_tjrjsj_jiekou()
	{
		$result = db('churu_jiekou')->where('churu_lb',1)->whereOr('churu_lb',2)->select();
		return $result;
	}

	public function cjmb_tjrjsj(Request $request)
	{
		$data = Request()->put();
		//halt($data);
		preg_match('/\d+/',$data['jiekou'],$jiekou);
		$jiekou_id = $jiekou[0];
		$shuju[] = ['chujin_zu_id'=>$data['moban_id'],'churu_id'=>$jiekou_id,'wap_bz'=>0];
		//halt($shuju);
		$result_detail = db('chujin_zu_detail')->insertAll($shuju);
		$result = array($result_detail,$data['moban_id']);
		return $result;
	}

	public function cjmb_xgrjsj_jiekou()
	{
		$result = db('churu_jiekou')->where('churu_lb',1)->whereOr('churu_lb',2)->select();
		return $result;
	}

	public function cjmb_xgrjsj(Request $request)
	{
		$data = Request()->put();
		//halt($data);
		preg_match('/\d+/',$data['jiekou'],$jiekou);
		$jiekou_id = $jiekou[0];
		//halt($jiekou_id);
		$result_detail = db('chujin_zu_detail')->where('id',$data['shuju_id'])->update(['churu_id'=>$jiekou_id]);
		$result = array($result_detail,$data['moban_id']);
		return $result;
	} 
	*/

}

